<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-05-17
 * Time: 08:46
 */

namespace Org\Net;

class myCode {

    public function __construct() {
        vendor("phpqrcode.phpqrcode");
    }

    static public function getInstance() {
        static $ins;
        if (false == $ins instanceof self) {
            $ins = new self;
        }
        return $ins;
    }

    public function getCode() {
        return new \QRcode();
    }

    static public function code62($x) {
        $show = '';
        while ($x > 0) {
            $s = $x % 62;
            if ($s > 35) {
                $s = chr($s + 61);
            } elseif ($s > 9 && $s <= 35) {
                $s = chr($s + 55);
            }
            $show .= $s;
            $x = floor($x / 62);
        }
        return $show;
    }

    function shorturl1($url) {
        $url = crc32($url);
        $result = sprintf("%u", $url);
        //return $url;
        //return $result;
        return self::code62($result);
    }

    static public function shorturl($input) {
        $base32 = array(
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z', '0', '1', '2', '3', '4', '5'
        );

        $hex = md5($input);
        $hexLen = strlen($hex);
        $subHexLen = $hexLen / 8;
        $output = array();

        for ($i = 0; $i < $subHexLen; $i++) {
            //把加密字符按照8位一组16进制与0x3FFFFFFF(30位1)进行位与运算
            $subHex = substr($hex, $i * 8, 8);
            $int = 0x3FFFFFFF & (1 * ('0x' . $subHex));
            $out = '';

            for ($j = 0; $j < 6; $j++) {

                //把得到的值与0x0000001F进行位与运算，取得字符数组chars索引
                $val = 0x0000001F & $int;
                $out .= $base32[$val];
                $int = $int >> 5;
            }

            $output[] = $out;
        }

        return $output;
    }

    public function enCrypt($string) {
        $array = str_split($string, '3');
        $count = count($array);

        $str_62 = '';
        foreach ($array as $key => $value) {
            $arr_62 = self::dec2s4($value);
            if ($key == ($count - 1)) {
                $str_62 .= str_pad($arr_62[0], 2, "0", STR_PAD_LEFT) . $arr_62[1];
            } else {
                $str_62 .= str_pad($arr_62[0], 2, "0", STR_PAD_LEFT);
            }
        }
        return $str_62;
    }

    public function deCrypt($string) {
        $array = str_split($string, '2');
        $count = count($array);

        $str_10 = '';
        foreach ($array as $key => $value) {
            $arr_10 = self::s42dec($value);
            //dump($arr_10);die;
            if ($key == ($count - 2)) {
                $str_10 .= str_pad($arr_10, $array[$key - 1], "0", STR_PAD_LEFT);
            } elseif ($key == ($count - 1)) {
                $str_10 = $str_10;
            } else {
                $str_10 .= str_pad($arr_10, 3, "0", STR_PAD_LEFT);
            }
        }
        return $str_10;
    }

    /**
     * 六十二进制
     * @param type $dec
     * @return string 
     */
    function dec2s4($dec) {
        $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $result = array();
        $result[1] = strlen($dec);
        do {
            $result[0] = $base[$dec % 62] . $result[0];
            $dec = intval($dec / 62);
        } while ($dec != 0);

        return $result;
    }

    /*
     * 六十四进制转为十进制
     */

    function s42dec($sixty_four) {
        $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $base_map = array_flip(str_split($base));
        $result = 0;
        $len = strlen($sixty_four);

        for ($n = 0; $n < $len; $n++) {
            $result *= 62;
            $result += $base_map[$sixty_four{$n}];
        }

        return $result;
    }

}